package com.chookss.tiku

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.chookss.R
import com.chookss.home.myTest.MyTestsDetailActivity
import com.chookss.tiku.adapter.LeftListAdapter
import com.chookss.tiku.adapter.RightListAdapter
import com.chookss.tiku.entity.LeftEntiry
import com.chookss.tiku.entity.MyExam
import com.chookss.tools.old.SignUtils
import com.johnrambo.ktea.ktExtends.getViewModel
import com.johnrambo.ktea.ktExtends.observe
import com.lvgroup.hospital.base.BaseAct
import infoToast
import kotlinx.android.synthetic.main.activity_select_simulation_item.*
import kotlinx.android.synthetic.main.common_title.*
import java.text.SimpleDateFormat
import java.util.*

/**
  *模拟卷 选择一级页面
 */
@RequiresApi(Build.VERSION_CODES.N)
class SelectSimulationCatalog : BaseAct(), View.OnClickListener{

    private var examCode:String =""

    private val model by lazy { getViewModel<SelectSimulationModel>() }

    //private val leftAdapter by lazy { LeftListAdapter() }
    private val leftAdapter by lazy {
        LeftListAdapter().apply {
            listener = { item ->
                tvSimulateChangyong.setBackgroundColor(Color.parseColor("#F7F7F7"))
                tvSimulateChangyong.isSelected=false
                initRight(item)
            }
        }
    }
    private val rightAdapter by lazy {
        RightListAdapter().apply {
            listener = { item ->
                examCode = item.catalogCode
                if(examCode.isEmpty()){
                    infoToast("请选择一项考试内容")
                }else{
                    clickOnExam(examCode)
                }
            }
        }
    }

    private fun clickOnExam(examCode: String) {
        model.getExamInfo(examCode).observe(this, {
            if (it.data != null) {
                var exam = MyExam()
                exam.startTime = SimpleDateFormat(MyExam.StartTimeFormat).format(Date())
                exam.examName = it.data.examName
                exam.examCode = it.data.examCode
                exam.examTimeLength = it.data.examTimeLength
//                var intent = Intent(this, SimulateTestActivity::class.java)
//                intent.putExtra("exam", exam)
//                intent.putExtra("recordType", "3")
//                intent.putExtra("answerType", "2")
                var intent = Intent(this, MyTestsDetailActivity::class.java)
                intent.putExtra("exam", exam)
                intent.putExtra("examCode",  exam.examCode)
                intent.putExtra("examStatus",  "102")
                intent.putExtra("recordType", "3")
                startActivity(intent)
                finish()
            }
        })
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_simulation_item)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun initData() {
        initState()
        common_title_txt.setText("模拟卷")
        tvSimulateChangyong.setBackgroundColor(Color.parseColor("#ffffff"))
        tvSimulateChangyong.isSelected=true
        common_title_back.setOnClickListener(this)
        tvSimulateChangyong.setOnClickListener(this)
        initLeft()
        initCyRight()
    }

    @RequiresApi(Build.VERSION_CODES.N)
    private fun initLeft() {
        rcLeft.layoutManager = LinearLayoutManager(this@SelectSimulationCatalog);
        rcLeft.adapter = leftAdapter
        val params1: MutableMap<String, Any> = HashMap()
        params1["catalogType"] = "2"; //目录类型,1专项练习 2 模拟卷
        params1["lvlNo"] = "1";
        val timeStamp1 = SignUtils.getTimeStamp() //20201208125238
        val randomString1 = SignUtils.getRandomString(14)
        val makeSignStr1 = SignUtils.makeSignStr(params1, timeStamp1, randomString1, false)

        model.getLeft("2", timeStamp1, randomString1, makeSignStr1)
        model.leftLiveData.observe(this) {
            leftAdapter.update(it)
        }
    }


    @RequiresApi(Build.VERSION_CODES.N)
    private fun initRight(item: LeftEntiry) {
        rcRight.layoutManager = LinearLayoutManager(this@SelectSimulationCatalog);
        rcRight.adapter = rightAdapter

        val params2: MutableMap<String, Any> = HashMap()
        params2["catalogCode"] = item.catalogCode;
        val timeStamp2 = SignUtils.getTimeStamp() //20201208125238
        val randomString2 = SignUtils.getRandomString(14)
        val makeSignStr2 = SignUtils.makeSignStr(params2, timeStamp2, randomString2, false)

        model.getRight(item.catalogCode, timeStamp2, randomString2, makeSignStr2)
        model.rightLiveData.observe(this) {
            rightAdapter.update(tolvl2List(it))
        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    private fun initCyRight() {
        rcRight.layoutManager = LinearLayoutManager(this@SelectSimulationCatalog);
        rcRight.adapter = rightAdapter
//        rcRight.addItemDecoration(RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5f), resources.getColor(R.color.line_gray)))
        val params3: MutableMap<String, Any> = HashMap()
        val timeStamp3 = SignUtils.getTimeStamp() //20201208125238
        val randomString3 = SignUtils.getRandomString(14)
        val makeSignStr3 = SignUtils.makeSignStr(params3, timeStamp3, randomString3, false)

        model.getCyRight(timeStamp3, randomString3, makeSignStr3)
        model.rightCyLiveData.observe(this) {
            rightAdapter.update(tolvl2List(it))
        }
    }

    fun tolvl2List(childList:List<LeftEntiry>):List<LeftEntiry> {
        var list = ArrayList<LeftEntiry>()
        childList.forEach {
            if (it.childList == null || it.childList.isEmpty()) {
                list.add(it.apply { lvlNo = 2 })
            } else {
                list.add(it.apply { lvlNo = 1 })
                it.childList?.forEach {
                    list.add(it.apply { lvlNo = 2 })
                }
            }
        }
        return list
    }


    override fun onClick(v: View?) {
        when(v?.id){
            R.id.common_title_back->{
                finish()
            }
            //列表最上面的 常用
            R.id.tvSimulateChangyong->{
                tvSimulateChangyong.setBackgroundColor(Color.parseColor("#ffffff"))
                tvSimulateChangyong.isSelected=true
                initCyRight()
                initLeft()
            }
        }
    }



}